package com.bdqn.intereceptior;

import com.bdqn.entity.Employee;
import com.bdqn.util.Constants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 身份校验拦截器
 */
@Component
@Slf4j
public class Authentication implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        log.info("拦截器拦截的信息为: {}",request.getRequestURI());
        log.info("获取该次请求的session: {}",request.getSession().getAttribute(Constants.LOGIN_SESSION));
        String login= (String) request.getSession().getAttribute(Constants.LOGIN_SESSION);
        if(!StringUtils.isEmpty(login)){
            return true;
        }else {
            //跳转到登录界面
            response.sendRedirect(request.getContextPath()+"/backend/page/login/login.html");
            return false;
        }

    }
}
